package org.example.project.bookstore.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import io.jsonwebtoken.Claims;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;
import org.example.bean.Book;
import org.example.config.JwtConfig;
import org.example.project.bookstore.service.BookService;
import org.example.project.bookstore.service.impl.ShoppingCartServiceImpl;
import org.example.project.bookstore.service.impl.UserServiceImpl;
import org.example.util.JsonResult;
import org.springframework.data.redis.core.HashOperations;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * @Auther:Liu
 * @Testname:BookController
 * @Date:2025/8/18 10:12
 */
@RestController
@CrossOrigin
@Slf4j
@RequestMapping("/new_book")
public class BookController {

    @Resource
    private BookService bookService;


    //获取低价书籍
    @GetMapping("getlowerbook/{number}")
    JsonResult getlowerbook(@PathVariable("number") int number) {
        return bookService.findLower(number);
    }

    //获取最新书籍
    @GetMapping("getnewbook/{number}")
    JsonResult getnewbook(@PathVariable("number") int number) {
        return bookService.getNewBook(number);
    }

    //查询新鲜好书
    @GetMapping("/getAllNewBook/{page}")
    JsonResult getAllNewBook(@PathVariable("page") int page, HttpServletRequest request) {
        String token = request.getHeader("token");
        log.info("token:{}", token);
        JsonResult<IPage<Book>> result = bookService.getAllNewBook(page, request.getHeader("token"));
        IPage<Book> data = result.getData();
        List<Book> list = data.getRecords();
        List<Book> list1 =checkExistsCart(list,request.getHeader("token"));
        data.setRecords(list1);
        return result;
    }

    //    按分类查找图书
    @GetMapping("/findBookByCateGoryId/{page}/{categoryId}")
    JsonResult findBookByCateGoryId(@PathVariable("page") int page, @PathVariable("categoryId") String categoryId, HttpServletRequest request) {
        JsonResult<IPage<Book>> result = bookService.findBookByCateGory(page, categoryId, request.getHeader("token"));
        IPage<Book> data = result.getData();
        List<Book> list = data.getRecords();
        List<Book> list1 =checkExistsCart(list,request.getHeader("token"));
        data.setRecords(list1);
        return result;
    }

    //一级模糊查询
    @GetMapping("/findBookByName/{page}/{bookName}")
    JsonResult findBookByName(@PathVariable("page") int page, @PathVariable("bookName") String bookName, HttpServletRequest request) {
        JsonResult<IPage<Book>> result = bookService.findBookByName(page, bookName, request.getHeader("token"));
        IPage<Book> data = result.getData();
        List<Book> list = data.getRecords();
        List<Book> list1 =checkExistsCart(list,request.getHeader("token"));
        data.setRecords(list1);
        return result;
    }

    //二级模糊查询
    @GetMapping("/findBookByCategoryAndPrice/{page}/{categoryId}/{bookName}")
    JsonResult findBookByCategoryAndPrice(@PathVariable("page") int page, @PathVariable("categoryId") String categoryId, @PathVariable("bookName") String bookName, HttpServletRequest request) {
        JsonResult<IPage<Book>> result = bookService.findBookByCategoryAndPrice(page, categoryId, bookName,request.getHeader("token"));
        IPage<Book> data = result.getData();
        List<Book> list = data.getRecords();
        List<Book> list1 =checkExistsCart(list,request.getHeader("token"));
        data.setRecords(list1);
        return result;
    }

    //二级模糊查询
    @GetMapping("/findBookByNames/{page}/{bookName}/{bookName2}")
    JsonResult findBookByNames(@PathVariable("page") int page, @PathVariable("bookName") String bookName, @PathVariable("bookName2") String bookName2, HttpServletRequest request) {
        JsonResult<IPage<Book>> result =  bookService.findBookByNames(page, bookName, bookName2,request.getHeader("token"));
        IPage<Book> data = result.getData();
        List<Book> list = data.getRecords();
        List<Book> list1 =checkExistsCart(list,request.getHeader("token"));
        data.setRecords(list1);
        return result;
    }

    //查询所有低价书
    @GetMapping("/getAllLowerBook/{page}")
    JsonResult findLowerMore(@PathVariable("page")int page, HttpServletRequest request) {
        JsonResult<IPage<Book>> result =  bookService.findLowerMore(page,request.getHeader("token"));
        IPage<Book> data = result.getData();
        List<Book> list = data.getRecords();
        List<Book> list1 =checkExistsCart(list,request.getHeader("token"));
        data.setRecords(list1);
        return result;
    }


    //二级模糊查询低价书
    @GetMapping("/findLowerAndBooknameMore/{page}/{bookname}")
    JsonResult findLowerAndBooknameMore(@PathVariable("page")int page, @PathVariable("bookname")String bookname, HttpServletRequest request) {
        JsonResult<IPage<Book>> result =  bookService.findLowerAndBooknameMore(page, bookname,request.getHeader("token"));
        IPage<Book> data = result.getData();
        List<Book> list = data.getRecords();
        List<Book> list1 =checkExistsCart(list,request.getHeader("token"));
        data.setRecords(list1);
        return result;
    }

    //二级模糊查询新鲜好书
    @GetMapping("/findNewBookByNameAndTime/{page}/{bookname}")
    JsonResult findNewBookByNameAndTime(@PathVariable("page") int page, @PathVariable("bookname") String bookName, HttpServletRequest request){
        JsonResult<IPage<Book>> result =   bookService.findNewBookByNameAndTime(page, bookName,request.getHeader("token"));
        IPage<Book> data = result.getData();
        List<Book> list = data.getRecords();
        List<Book> list1 =checkExistsCart(list,request.getHeader("token"));
        data.setRecords(list1);
        return result;
    }

    @Resource
    private StringRedisTemplate stringRedisTemplate;

    public List<Book> checkExistsCart(List<Book> list, String token) {
        boolean b = JwtConfig.checkToken(token);
        if (!b) {
            return list;
        }
        Claims claims = JwtConfig.parseJWT(token);
        Object userId = claims.get("id");
        ValueOperations<String, String> ops = stringRedisTemplate.opsForValue();
        String s = ops.get(UserServiceImpl.LOGIN_PREFIX + userId);
        if (!Objects.equals(s, token)) {
            return list;
        }
        HashOperations<String, Object, Object> ops1 = stringRedisTemplate.opsForHash();
        Map<Object, Object> entries = ops1.entries(ShoppingCartServiceImpl.SHOPPING_CART + userId);
        for (Book book : list) {
            boolean b1 = entries.containsKey(book.getId());
            if (b1) {
                book.setShow(Boolean.TRUE);
            }
        }
        return list;
    }
}
